namespace Vanilla.UI

import System

// WINDOW PREVIEW
class WindowPreview (PanelItem):
	public Window as Window
	
	override MinSize as Size:
		get:
			if Window is null or Parent is null:
				return Size(10,10)
			else:
				return Size((Window.Size.Width/Window.Size.Height) * (Parent.Size.Height-Parent.Spacing*2), (Parent.Size.Height-Parent.Spacing*2))

	LastWindowSize as Size
	
	OnClick as callable = {Window.BringToFront()}
	OnRightClick as callable = {PopUp(Window.Menu)}
	
	def constructor (p as Box, w as Window):
		Window = w
		super(p)
		
		// Every 10 seconds, redraw and check the size
		UITimer(10000) def (t as UITimer):
			if Parent == null:
				t.Stop()
			elif Window.Size != LastWindowSize:
				Parent.Parent.Pack()
				LastWindowSize = Window.Size
			else:
				Redraw()
	
	def Draw (b as Brush):
		b.Scale(Size.Width / Window.Size.Width, Size.Height / Window.Size.Height)
		Window.Draw(b)
		unless ContainsMouse or Window.ContainsMouse:
			b.Draw(Rect(Window.Size))
			b.Colour = Colour(1,1,1,0.25)
			b.Fill()
	
	override def Click (b as MouseButton, x as double, y as double):
		print "CLICKY"
		if b == MouseButton.Right:
			OnRightClick()
		elif b == MouseButton.Left:
			OnClick()
	
	override def MouseEnter ():
		PopUp(ToolTip("Shoop da whoop?"))

// WINDOW
class Window (TopLevel):
	// RECT
	/*override Rect as Rect:
		set:
			old = Size
			super(value)
			if Size.Width != old.Width or Size.Height != old.Height:
				unless Preview == null or Preview.Parent == null:
					Preview.Parent.Pack()*/

	// LABEL
	class Label (Vanilla.UI.Label):
		def constructor (p as Box, t as string):
			super(p,t)
	
	override def BringToFront ():
		Order = Desktop.Panel.Order-1
		Redraw()
	
	// CLOSE BUTTON
	class CloseButton (Button):
		override MinSize as Size:
			get:
				return Size(15,15)
	
		def constructor (p as Box, c as callable):
			super(p,c)
	
	// MENU BUTTON
	class MenuButton (Button):
		override MinSize as Size:
			get:
				return Size(15,15)
		
		def constructor (p as Box, w as Window):
			super(p,{PopUp(w.Menu)})
	
	// PREVIEW
	public Preview as WindowPreview
	public AutoUpdatePreview as bool = true
	
	// PROPERTIES
	public RightButtons as Row
	public LeftButtons as Row
	
	public TitleBar as Label
	
	public Menu as Menu

	Title as string:
		get:
			return TitleBar.Text unless TitleBar == null
			return "No title."
		set:
			TitleBar.Text = value unless TitleBar == null
			TitleBar.Redraw()
	
	public CanResize = true
	
	public BottomOfTop as double
	
	override MinSize as Size:
		get:
			return super() if Child == null
			return Size(Math.Max(_MinSize.Width, Child.MinSize.Width + Spacing*2),
			            Math.Max(_MinSize.Height, Child.MinSize.Height + Spacing + BottomOfTop +3))
	
	// CONSTRUCTORS
	def constructor (p as Desktop, t as string):
		super(p)
		
		PausePacking()
		
		Size = Size(200,200)
		
		TitleBar = Label(self, t)
		
		RightButtons = Row(self)
		RightButtons.DrawFrame = false
		
		CloseButton(RightButtons, Close)
		
		LeftButtons = Row(self)
		LeftButtons.DrawFrame = false
		
		MenuButton(LeftButtons, self)
		LeftButtons.Pack()
		
		Menu = Menu()
		Menu.AddItem("Window", "Close", Close)
		Menu.AddItem("Window", "Save screenshot [in 2 seconds]") def ():
			UITimer (2000) def (t as UITimer):
				using c = BitmapCanvas(Size.Width,Size.Height):
					using b = Brush(c):
						Draw(b)
					c.WriteToPNG("screenshot.png")
				t.Stop()
				AlertWindow("Written to screenshot.png")
		
		child = null
		
		Preview = WindowPreview(p.Panel.Right, self)
		
		ResumePacking()
		
		BringToFront()
	
	def constructor (t as string):
		self(Tool.CurrentDesktop, t)
	
	// ARRANGE
	def Arrange ():
		BottomOfTop = 15.0
		
		BottomOfTop = Math.Max(BottomOfTop, TitleBar.MinSize.Height+3) unless TitleBar == null
		BottomOfTop = Math.Max(BottomOfTop, RightButtons.MinSize.Height+3) unless RightButtons == null
		BottomOfTop = Math.Max(BottomOfTop, LeftButtons.MinSize.Height+3) unless LeftButtons == null
		
		unless TitleBar == null:
			TitleBar.Rect = Vanilla.UI.Rect(3,3,Size.Width-3,BottomOfTop)
		
		unless RightButtons == null:
			RightButtons.Rect = Vanilla.UI.Rect(Size.Width-3-RightButtons.MinSize.Width, 3, Size.Width-3, BottomOfTop)
		
		unless LeftButtons == null:
			LeftButtons.Rect = Vanilla.UI.Rect(3, 3, 3+LeftButtons.MinSize.Width, BottomOfTop)
		
		unless Child == null:
			Child.Rect = Vanilla.UI.Rect(Spacing, BottomOfTop + Spacing, Size.Width-Spacing, Size.Height-Spacing)
	
	// REDRAW AREA
	override def RedrawArea (r as Rect):
		super(r)
		//unless Preview == null:
		//	if AutoUpdatePreview:
		//		Preview.Redraw()//Area(Rect(r.X/Preview.Size.Width, r.Y/Preview.Size.Height, r.X2/Preview.Size.Width, r.Y2/Preview.Size.Height))
	
	// WINDOW EVENTS
	virtual def Close ():
		Parent.Remove(self) unless Parent == null
		Preview.Parent.Remove(Preview) unless Preview == null or Preview.Parent == null
	
	// MOUSE EVENTS
	public BeingMoved = false
	drag_x as double
	drag_y as double
	
	public BeingResized = false
	resize_x as double
	resize_y as double
	
	override def MouseDown (b as MouseButton, x as double, y as double):
		BringToFront()
	
		if ChildAtMousePosition(x,y) != null:
			super(b,x,y)
		elif b == MouseButton.Left:
			if x > Size.Width-25 and y > Size.Height-25 and CanResize:
				BeingResized = true
				GrabMouse()
				resize_x = Size.Width-x
				resize_y = Size.Height-y
			else:
				BeingMoved = true
				GrabMouse()
				drag_x = x
				drag_y = y
	
	override def ContainsMouseAt (x as double, y as double):
		return Size.Contains(x,y)
		
	override def Contains (x as double, y as double):
		return Size.Contains(x,y)
		
	override def MouseUp (b as MouseButton, x as double, y as double):
		if BeingMoved:
			BeingMoved = false
			ReleaseMouse()
			drag_x = drag_y = 0
		elif BeingResized:
			BeingResized = false
			ReleaseMouse()
			resize_x = resize_y = 0
		else:
			super(b,x,y)
	
	override def MouseMotion (x as double, y as double):
		if BeingMoved:
			Position = Position(Position.X + x - drag_x, Position.Y + y - drag_y)
		elif BeingResized:
			Size = Size(Math.Max(MinSize.Width, x + resize_x), Math.Max(MinSize.Height, y + resize_y))
		else:
			super(x,y)

class PopupWindow (Window):
	def constructor (p as Desktop, t as string):
		super(p,t)
	
	def constructor (t as string):
		super(t)

class AlertWindow (PopupWindow):
	def constructor (t as string, cb as callable):
		super("Notification")
		c = Column(self)
		Label(c,t)
		Button(c, "OK", {Close(); cb()})
		Size = MinSize
	
	def constructor (t as string):
		self(t, {print ""})

class ErrorWindow (PopupWindow):
	def constructor (t as string, cb as callable):
		super("Error")
		c = Column(self)
		Label(c,t)
		Button(c, "OK", {Close(); cb()})
		Size = MinSize
	
	def constructor (t as string):
		self(t, {print ""})

// MAIN WINDOW
class MainWindow (Window):
	def constructor (p as Desktop, t as string):
		super(p, t)
	
	def constructor (t as string):
		super(t)
